var app = getApp(); //
import { PNT, setNavBarTitle, showToast, shopUrl, formatLeftTime, pushPagePath, formatTime } from "../../utils/utils";
import { FlashSale } from '../../utils/apis';
Page({
  data: {
    time: [
      {
        id: 1,
        sj: '8:00',
        static: 1,
      },
      {
        id: 2,
        sj: '10:00',
        static: 2,
      },
      {
        id: 3,
        sj: '12:00',
        static: 2,
      },
      {
        id: 4,
        sj: '14:00',
        static: 2,
      },
      {
        id: 5,
        sj: '16:00',
        static: 2,
      },
    ],
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    expertList: [
      [
        {
          img: "https://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/201703/goods_img/0_G_1490147626763.jpg",
          name: "Galanz/格兰仕 G80F23CN3L-C2(R0)正品微波炉23L 智能光波炉家用 800w速热23升容量1级能效智能预约",
          shop_price: 100.00,
          tj: 50.00,
          gs: 2,
          static: 1,
        },
        {
          img: "http://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/201703/thumb_img/0_thumb_G_1490209796384.jpg",
          name: "航科家用按摩椅全自动全身电动多功能太空舱按摩器老人沙发椅 智能大腿气囊按摩 脚底3D推拿",
          shop_price: 100.00,
          tj: 50.00,
          gs: 20,
          gml: 8,
          static: 2,

        },

      ],
      [
        {
          img: "https://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/201703/goods_img/0_G_1490147626763.jpg",
          name: "Galanz/格兰仕 G80F23CN3L-C2(R0)正品微波炉23L 智能光波炉家用 800w速热23升容量1级能效智能预约",
          shop_price: 100.00,
          tj: 50.00,
          gs: 20,
          gml: 8,
          static: 2,

        },
      ],
    ]
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.currentTarget.dataset.id;

    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }

    var self = this;
    var data = {};
    data.time = e.currentTarget.dataset.id;
    FlashSale(data).then(res => {
      if (res.error_code == 10000) {
        if (res.data.length > 0) {
          this.setData({
            time: res.time,
            goods: res.data,
            now:res.now,
          })
        } else {
          self.setData({
            goods:[],
          })
          wx.showToast({
            title: res.msg,
          })
        }
      } else {
        self.setData({
          goods: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'none',
        })
      }
    })

  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function () {
    var that = this;
    this.loginModal = this.selectComponent("#login-modal");
    if(!this.loginModal.check()){
      return false;
    }
    // 高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    // this.flashSale();
  },

  tiaozhuan: function (e) {
    var status = e.currentTarget.dataset.status;
    if(status){
      wx.navigateTo({
        url: '/pages/seckill/detail?id=' + e.currentTarget.dataset.id + "&tbid=" + e.currentTarget.dataset.tbid,
      })
    }else{
      wx.navigateTo({
        url: '/pages/goods/detail/detail?id=' + e.currentTarget.dataset.id,
      })
    }
  },
  flashSale(e){
    var self = this;
    var data = {};
    data.time = false;
    FlashSale(data).then(res=>{
      if(res.error_code == 10000){
        if(res.data.length > 0){
          this.setData({
            time:res.time,
            goods:res.data,
            now:res.now,
            currentTab:res.now.id
          })
        }else{
          wx.showToast({
            title: res.msg,
          })
        }
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none',
        })
      }
    })
  },
  switchFlash(e){

  },
  onShow:function(){
    this.flashSale();
  }

})